# -*- coding: utf-8 -*-
import os
from functools import lru_cache
from pathlib import Path

from pydantic import BaseSettings

# 获取项目根目录
# 或使用绝对路径，指到backend目录为止，例如windows：BasePath = D:\git_project\fastapi_mysql\backend
BasePath = Path(__file__).resolve().parent.parent

# 迁移文件存放路径
Versions = os.path.join(BasePath, "app", "alembic", "versions")

# 日志文件路径
LogPath = os.path.join(BasePath.parent, "log")

# 图片上传存放路径: /static/media/uploads/
ImgPath = os.path.join(BasePath, "app", "static", "media", "uploads")

# 头像上传存放路径: /static/media/uploads/avatars/
AvatarPath = os.path.join(ImgPath, "avatars", "")


class Settings(BaseSettings):
    """配置类"""

    # sqlite path
    SqlitePath = os.path.join(BasePath.parent.parent, "sqlite_db")

    JOBS_SQLITE_PATH = os.path.join(BasePath.parent.parent, "sqlite_db", "jobs.sqlite")

    TASK_PATH = os.path.join(BasePath.parent.parent, "task")

    # tornado template
    TORNADO_TEMPLATE_PATH = os.path.join(BasePath.parent.parent, "tornado_template")

    # 原生的图片地址
    # 标注好的图片地址
    IMG_RAW_PATH = os.path.join(BasePath.parent.parent, "uploads", "raw")
    IMG_DRAW_PATH = os.path.join(BasePath.parent.parent, "uploads", "draw")

    AGV_LOC_PATH = os.path.join(BasePath.parent.parent, "agv_param", "location.json")
    AGV_ALL1_INFO_PATH = os.path.join(BasePath.parent.parent, "agv_param", "all1.json")

    AGV_STATUS_PATH = os.path.join(BasePath.parent.parent, "agv_param", "status.json")
    AGV_MAP_PATH = os.path.join(BasePath.parent.parent, "agv_param", "map_points.json")

    shell_getuserinfo_path = os.path.join(BasePath.parent, "sh_script", "getuserinfo.ps1")
    shell_authkey_path = os.path.join(BasePath.parent, "sh_script", "getauthkey.ps1")

    AUO_LOGIN_PATH = os.path.join(BasePath.parent, "sh_script", "auologin", "auologin.exe")

    # FastAPI
    TITLE: str = "FastAPI"
    VERSION: str = "v0.0.1"
    DESCRIPTION: str = """fastapi_sqlalchemy_mysql"""
    DOCS_URL: str = "/v1/docs"
    REDOCS_URL: str = None
    OPENAPI_URL: str = "/v1/openapi"

    # Uvicorn
    UVICORN_HOST: str = "127.0.0.1"
    UVICORN_PORT: int = 8000
    UVICORN_RELOAD: bool = True
    # 如果此处为True，在 @app.on_event("startup") 时发生异常，则程序不会终止，详情：https://github.com/encode/starlette/issues/486

    # Static Server
    STATIC_FILES: bool = True

    # DB
    DB_ECHO: bool = False
    DB_HOST: str = "127.0.0.1"
    DB_PORT: int = 3306
    DB_USER: str = "root"
    DB_PASSWORD: str = "123456"
    DB_DATABASE: str = "fsm"
    DB_CHARSET: str = "utf8mb4"

    # Token
    TOKEN_ALGORITHM: str = "HS256"  # 算法
    TOKEN_SECRET_KEY: str = "1VkVF75nsNABBjK_7-qz7GtzNy3AMvktc9TCPwKczCk"  # 密钥 secrets.token_urlsafe(32))
    TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 1  # token 时效 60 * 24 * 1 = 7 天

    # Email
    EMAIL_DESCRIPTION: str = "fastapi_sqlalchemy_mysql"  # 默认发件说明
    EMAIL_SERVER: str = "smtp.qq.com"
    EMAIL_PORT: int = 465
    EMAIL_USER: str = "729519678@qq.com"
    EMAIL_PASSWORD: str = "gmrvkkppberzbega"  # 授权密码，非邮箱密码
    EMAIL_SSL: bool = True

    # 邮箱登录验证码过期时间
    EMAIL_LOGIN_CODE_MAX_AGE: int = 60 * 2  # 时效 60 * 2 = 2 分钟

    # Cookies
    COOKIES_MAX_AGE: int = 60 * 5  # cookies 时效 60 * 5 = 5 分钟

    # Middleware
    MIDDLEWARE_CORS: bool = True
    MIDDLEWARE_GZIP: bool = True
    MIDDLEWARE_ACCESS: bool = False

    # check user login
    LOGIN_URL = "http://10.12.14.120/tornado-cloud/tornadoLogin/checkLogin"
    LOGIN_CORP_ID = "24338c6f0efd6c4bfb28f2ccbf9df6a4"
    LOGIN_API_KEY = "93ecd06e-8d1b-48f1-a8d6-cfe5ededaacb"
    # the ip of agv
    AGV1_IP: str = "10.13.32.109"
    AGV1_NAME: str = "ZTX_AGV1"

    AGV2_IP: str = "10.13.32.110"
    AGV2_NAME: str = "ZTX_AGV2"

    AGV3_IP: str = "10.13.32.111"
    AGV3_NAME: str = "ZTX_AGV3"

    AGV4_IP: str = "10.13.32.73"
    AGV4_NAME: str = "FORK_AGV1"

    AGV5_IP: str = "10.13.32.11"
    AGV5_NAME: str = "NAV_AGV1"

    AGV_TABLE: dict = {AGV1_IP: AGV1_NAME, AGV2_IP: AGV2_NAME, AGV3_IP: AGV3_NAME, AGV4_IP: AGV4_NAME, AGV5_IP: AGV5_NAME}

    # the ip of agv
    AGV_IP: str = "10.13.32.11"
    # AGV_IP: str = "10.13.32.73"
    AGV_PORT = {"status": 19204, "control": 19205, "nav": 19206, "config": 19207, "other": 19210, "push": 19301, "unknown": 19208}
    # 需要丢掉的文字
    ocr_keyword_drop = [":"]
    ocr_keyword_save = ["溫度", "濕度", "温度", "湿度"]

    # state的颜色字典
    state_color = {
        "运行": "#87dc64",  # 运行，绿色
        "闲置": "#ffe664",  # 闲置，黄色
        "宕机": "#ff0000",  # 宕机，红色
        "低温": "#fff143",  # 低温，蓝色
        "高温": "#f20c00",  # 高温，红色
        "维护调试": "#e9f1f6",  # 维护，白色
        "维护": "#e9f1f6",  # 维护，白色
        "调试": "#e9f1f6",  # 维护，白色
    }

    # 蓝色圆盘文字过滤,不需要的关键字
    state_filter_save = ["运行", "闲置", "维护", "高温", "低温", "宕机", "调试"]
    # short
    SHORT_CAMERA_URL = "http://10.13.32.11:8001/camera/picture"
    # temp humi
    TEMP_HUMI_URL = "http://10.13.32.11:8002/sensor/temphumi"
    # long
    LONG_CAMERA_URL = "http://10.13.32.11:8002/camera/picture"
    # scheduler event flag
    START_EVENT = True
    # 连接AGV
    CONNECT_AGV = True

    def get_map_path(self, ip: str):
        """_summary_

        Args:
            ip (str): _description_

        Returns:
            _type_: _description_
        """
        agv_name = self.AGV_TABLE.get(ip)
        return os.path.join(BasePath.parent.parent, "agv_param", f"{agv_name}_map.json")

    def get_all1_info_path(self, ip: str):
        """_summary_

        Args:
            ip (str): _description_

        Returns:
            _type_: _description_
        """
        agv_name = self.AGV_TABLE.get(ip)
        return os.path.join(BasePath.parent.parent, "agv_param", f"{agv_name}_all1.json")


@lru_cache
def get_settings():
    """读取配置优化写法"""
    return Settings()


ST = get_settings()
